package dao;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import model.a;
import util.ConnMySQL;

public class ADao {

    public int save(a a){
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet affectedSet = null;

        String idusers = null;
        try {
            conn = ConnMySQL.getConnection();
            String sql = "insert into answers(idQA, answers,answerserid) values(?, ?, ?)";
//			[!]注意这里的insert获取id的写法
            preStmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            preStmt.setString(1, a.getIdQA());
            preStmt.setString(2, a.getAnswers());
            preStmt.setString(3, a.getAnswererID());
            preStmt.executeUpdate();
            affectedSet = preStmt.getGeneratedKeys();
            if (affectedSet.next()) {
                idusers = affectedSet.getString(1);
                System.out.println(idusers);
            }
            return 1;
        } catch  (SQLDataException ex) {
            if (ex.getErrorCode()==1062){
                return 0;
            }
            ex.printStackTrace();
            return 0;
        }catch (Exception ex){
            ex.printStackTrace();
            return 0;
        }finally {
            if (affectedSet != null) {
                try {
                    affectedSet.close();
                    affectedSet = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            if (preStmt != null) {
                try {
                    preStmt.close();
                    preStmt = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }


    public List<a> findById(String idQA) throws Exception {
        Connection conn = null;
        PreparedStatement preStmt = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM answers where idQA=?";
        conn=ConnMySQL.getConnection();
        if (conn != null) {
            try {
                preStmt = conn.prepareStatement(sql);
                preStmt.setString(1, idQA);
                rs = preStmt.executeQuery();
                List<a> aList = rsToQa(rs);
                return aList;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    private List<a> rsToQa(ResultSet rs) {
        List<a> aList = new ArrayList<>();
        try {
            while (rs.next()) {
                String idQA = rs.getString("idQA");
                String answers = rs.getString("answers");
                String answerserId = rs.getString("answerserid");
                String Idanswers = rs.getString("idanswers");
                a a = new a(idQA,answers,answerserId,Idanswers);
                aList.add(a);
            }
            return aList;
        } catch (SQLException e) {
            processSqlError(e);
        }
        return null;
    }

    private void processSqlError(Exception e) {
        e.printStackTrace();
    }

    public static void main(String[] args) {

    }

}
